package com.yanyilu.qiubite.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.OSSClientBuilder;
import com.yanyilu.qiubite.jwt.JwtIgnore;
import com.yanyilu.qiubite.service.UploadService;
import com.yanyilu.qiubite.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RequestMapping("upload")
@RestController
public class UploadController {

    @Autowired
    UploadService uploadService;

    @Value("${uploadDir}")
    private String uploadDir;



/*    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${accessKeySecret}")
    private String accessKeySecret;
    @Value("${endpoint}")
    private String endpoint;*/


   /* *//**
     * 后台富文本框图片上传
     * @param file
     * @return
     * @throws RuntimeException
     *//*
    @JwtIgnore
    @PostMapping("/admin/image")
    public R adminUploadImage(@RequestParam("file") MultipartFile file) throws RuntimeException {
        if (file.isEmpty()) {
            return R.ok().put("message", "文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        //System.out.println(fileName);
        //privateln(fileName);
        // 如果是获取的含有路径的文件名，那么截取掉多余的，只剩下文件名和后缀名
        int index = fileName.lastIndexOf("_");
        if (index > 0) {
            fileName = fileName.substring(index + 1);
        }
        // 判断单个文件大于10M
        long fileSize = file.getSize();
        if (fileSize >= 1024 * 10240) {
            return R.ok().put("message", "文件大于10M");
        }
        // 当文件有后缀名时
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);

        //System.out.println(suffixName);
        if (!suffixName.equals("png") && !suffixName.equals("jpg") && !suffixName.equals("jpeg") && suffixName.equals("bmp") && !suffixName.equals("svg") && !suffixName.equals("gif") && !suffixName.equals("gfif")){

            return R.ok().put("message", "图片格式不正确!");
        }
        //System.out.println("fileName:" + fileName);

        // 文件上传后的路径
        String filePath = uploadDir;
        //System.out.println(uploadDir);
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        // 如果该文件的上级文件夹不存在，则创建该文件的上级文件夹及其祖辈级文件夹;
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        Map<String,String> data = new HashMap<>();
        data.put("src", downLoadUrl + fileName);

        try {
            file.transferTo(dest);
            return R.ok("图片上传成功").put("data", data);
        } catch (IllegalStateException e) {
            //e.printStackTrace();
            return R.error("图片上传失败");
        } catch (IOException e) {
            //e.printStackTrace();
            return R.error("图片上传失败");
        }catch (MaxUploadSizeExceededException e){
            return R.error("图片过大");
        }

    }*/
    /**
     * 编辑器的文件上传*/
    @JwtIgnore
    @PostMapping("/admin/image")
    public R uploadEdit(@RequestParam(value = "file") MultipartFile file) throws RuntimeException {
        if (file.isEmpty()) {
            return R.ok().put("message", "文件为空");
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = uploadDir;
        long time = System.currentTimeMillis() / 1000;
        String path = filePath + time + suffixName;
        File dest = new File(filePath + time + suffixName);
        String pth = "";
        try {
            file.transferTo(dest);
            pth = uploadService.toOSS(time + suffixName, path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> data = new HashMap<>();
        data.put("src",pth);
        return R.ok().put("message", "图片上传成功").put("data",data);
    }

}
